var util={
	
		getCurrentMonth:function(date){
      var dateArray = [];
      var beginDate = date;
      beginDate.setDate(1);

      var year = date.getFullYear();
      var month = date.getMonth()+1;
      var nextMonthFirstDay=new Date(year,month,1);
      var oneDay=1000*60*60*24;
      var lastDate = new Date(nextMonthFirstDay-oneDay);

      dateArray.push(beginDate);
      dateArray.push(lastDate);
      return dateArray;
    },
    //返回指定格式的时间
    getTime: function (date) {
        date = date || new Date();
        function formatNumber(n) {
            n = n.toString();
            return n[1] ? n : '0' + n
        }
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();

        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();

        return [formatNumber(year), formatNumber(month), formatNumber(day)].join('-') + ' ' + [formatNumber(hour), formatNumber(minute), formatNumber(second)].join(':')
    },

    getDate: function (date) {
        return this.getTime(date).split(" ")[0];
    },
    
    fmtSeconds:function(seconds) { 
    	 var theTime = parseInt(seconds);// 需要转换的时间秒 
    	 var theTime1 = 0;// 分 
    	 var theTime2 = 0;// 小时 
    	 var theTime3 = 0;// 天
    	 if(theTime > 60) { 
    	  theTime1 = parseInt(theTime/60); 
    	  theTime = parseInt(theTime%60); 
    	  if(theTime1 > 60) { 
    	   theTime2 = parseInt(theTime1/60); 
    	   theTime1 = parseInt(theTime1%60); 
    	   if(theTime2 > 24){
    	    //大于24小时
    	    theTime3 = parseInt(theTime2/24);
    	    theTime2 = parseInt(theTime2%24);
    	   }
    	  } 
    	 } 
    	 var result = '';
    	 if(theTime > 0){
    	  result = ""+parseInt(theTime)+"秒";
    	 }
    	 if(theTime1 > 0) { 
    	  result = ""+parseInt(theTime1)+"分"+result; 
    	 } 
    	 if(theTime2 > 0) { 
    	  result = ""+parseInt(theTime2)+"小时"+result; 
    	 } 
    	 if(theTime3 > 0) { 
    	  result = ""+parseInt(theTime3)+"天"+result; 
    	 }
    	 return result; 
    },
    
    addDays: function(date, days){
        return this.addTimes(date, 60, 60, 24, days);     
    },
    addHours: function(date, hours){
        return this.addTimes(date, 60, 60, hours, 1);      
    },
    addMinutes: function(date, minutes){
        return this.addTimes(date, 60, minutes, 1, 1);
    },
    addSeconds: function(date, secs){
        return this.addTimes(date, secs, 1, 1, 1);      
    },
    addTimes: function(date, s, m, h, days){
      //new时间对象
      date = date || new Date();
      //转换成毫秒
      var mt = date.getTime();
      //把毫秒时间对象减7天，转换成时间对象
      var premt = mt + 1000 * s * m * h * days;
      var dt = new Date(premt);
      return dt;        
    },
    
    addMonth:function(date,month){
    	var dt = date;
    	dt.setMonth(date.getMonth()+month);
    	return dt;
    },
    
    DateCompare: function(beginDate,endDate){
		var d1 = new Date(beginDate.replace(/\-/g, "\/"));    
		var d2 = new Date(endDate.replace(/\-/g, "\/")); 
		if(d1 >d2)
			return false;
		return true;
	},
	
	daysBetween:function(beginDate,endDate){
		var days  =  parseInt((endDate - beginDate) /  1000  / 60 / 60 /24);
		return days;		
	},
    
    //格式化时间  
    //参数: 时间对象 
    //      要转换换的格式 年:yyyy 月:MM 日: dd  时(24):HH 时(12):hh 分:mm 秒:ss 
    //返回值: 按传的参数输出字符串
    formatTime: function(obj, arg){
      function fixNum(num)
      {
        num = parseInt(num);
        if(num < 10)
          return "0" + num;
        return num;  
      }
      function to12Hour(num)
      {
         num = parseInt(num);
         if(num > 12)
         {
           return num - 12; 
         } 
         return num;
      }
      if(!obj)
        obj = new Date();
      var year = obj.getFullYear();
      var month = fixNum(obj.getMonth() + 1);
      var date = fixNum(obj.getDate());
      var hour24 = fixNum(obj.getHours());
      var hour12 = fixNum(to12Hour(obj.getHours()));
      var minute = fixNum(obj.getMinutes());
      var second = fixNum(obj.getSeconds());
      if(!arg)
      {
        return "" + year + month + date + hour24 + minute + second;  
      }
      arg = arg.replace(/yyyy/g, year);
      arg = arg.replace(/MM/g, month);
      arg = arg.replace(/dd/g, date);
      arg = arg.replace(/HH/g, hour24);
      arg = arg.replace(/hh/g, hour12);
      arg = arg.replace(/mm/g, minute);
      arg = arg.replace(/ss/g, second);
      return arg;      
    },
    getUserDate:function(date){  //yyyyMM
    	return new Date(date.substr(0,4)+"/"+date.substr(4,2));
    },
    
    filterArray:function(dataArray,extArray){
      var result = [];
      if(Array.isArray(dataArray)&&Array.isArray(extArray)){
        dataArray.forEach(function(item,index){
            if(extArray.indexOf(index)>-1){
              result.push(item);
            }
        });
      }
      return result;
    },

    excludeArray:function(dataArray,excArray){
     var result = [];
     if(Array.isArray(dataArray)&&Array.isArray(excArray)){
        dataArray.forEach(function(item,index){
            if(excArray.indexOf(index)==-1){
              result.push(item);
            }            
        });
      }
      return result;
    },
    
    excludeDictArray:function(dataArray,excArray){
    	var result = [];
    	if(Array.isArray(dataArray)&&Array.isArray(excArray)){
	    	dataArray.forEach(function(item,index){
	    		if(excArray.indexOf(item.code)==-1)
	    		result.push(item);
	    	});
    	}
    	return result;
    },

    getQueryString: function (queryStringName){
        var returnValue="";
        var URLString = window.location.href;
        var serachLocation=-1;
        var queryStringLength=queryStringName.length;
        do{
            serachLocation=URLString.indexOf(queryStringName+"\=");
            if (serachLocation!=-1)
            {
                if ((URLString.charAt(serachLocation-1)=='?') || (URLString.charAt(serachLocation-1)=='&'))
                {
                    URLString=URLString.substr(serachLocation);
                    break;
                }
                URLString=URLString.substr(serachLocation+queryStringLength+1);
            }

        }
        while (serachLocation!=-1)
        if (serachLocation!=-1){
            var seperatorLocation=URLString.indexOf("&");
            if (seperatorLocation==-1)
            {
                returnValue=URLString.substr(queryStringLength+1);
            }
            else
            {
                returnValue=URLString.substring(queryStringLength+1,seperatorLocation);
            }
        }
        return returnValue;
    },
    //element-ui 表单验证
    checkForm: function(form, rules, context){
      var b = true;
      for(var i in rules){
        form.validateField(i, function(e){
          if(e){
            context ? context.$alert(e) : alert(e);
            b = false;
          }
        })
        if(!b)
          return b;
      }
      return true;
  },
  //rgb转16进制
  colorRGB2Hex: function(color) {
    var rgb = color.split(',');
    var r = parseInt(rgb[0].split('(')[1]);
    var g = parseInt(rgb[1]);
    var b = parseInt(rgb[2].split(')')[0]);
    var hex = "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
    return hex;
  },
  inject: function(path){
    var getTypeName = function(fileName){
      var str = fileName.match(/\.([^\.]*)$/);
      return str ? str[1] : "";
    }    
    var goBack = function(tPath){
      if(tPath == "http://" || tPath == "https://" || tPath == "file:///"){
        return tPath;  
      }
      return tPath.replace(/\/[^\/]*$/, "");
    }
    if(document.readyState == "complete"){
      console.log("require method must be immediately run when loading file");
      return;
    }
    // 获取现在已经加载的所有script
    var scripts = document.getElementsByTagName('script');
    // 获取最近一个加载的script，即这个js本身
    scriptEle = scripts[scripts.length-1];
    // 获取此js的路径  
    var scriptSrc = scriptEle.src; 
    //去掉文件名
    scriptSrc = goBack(scriptSrc);
    var backArr = path.match(/^(\.\.\/)+/);
    var len = 0;
    if(backArr){
      len = backArr[0].split("/").length - 1;
    }
    var pathName = path.replace(/^(\.\.\/)+/, "");
    for(var i = 0; i < len; i++){
      scriptSrc = goBack(scriptSrc);
    }
    var rPath = scriptSrc + "/" + pathName
    var typeName = getTypeName(pathName);
    if(typeName.toLowerCase() == "css"){
      document.write('<link rel="stylesheet" href="' + rPath + '"/>');
    }
    else{
      rPath = typeName == "" ? rPath + ".js" : rPath;
      document.write('<script src="' + rPath + '"></script>');
    }
  },
  openDialog: function(url, title){
    var width = window.parent.document.body.clientWidth;
    var height = window.parent.document.body.clientHeight;
    title = title || "";
    lhgdialog.opendlg(title, url, width, height, true, true);
  },
  
  sensitiveWord: function(mgArr, v){
    for(var i = 0; i < mgArr.length; i++){
      var cName = mgArr[i].ORIENTATION == "1" ? "bg-light-blue" : "bg-dark-red";
      var str = mgArr[i].SENSITIVEWORDKEY;
      var reg = new RegExp("("+str+")", "g");
      v = v.replace(reg, '<span class="' + cName + '">' + "$1" + '</span>');
    }
    return v;
  },
  getuuid : function(len, radix) {
		var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
				.split('');
		var uuid = [], i;
		radix = radix || chars.length;

		if (len) {
			// Compact form
			for (i = 0; i < len; i++)
				uuid[i] = chars[0 | Math.random() * radix];
		} else {
			// rfc4122, version 4 form
			var r;

			// rfc4122 requires these characters
			uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
			uuid[14] = '4';

			// Fill in random data. At i==19 set the high bits of clock sequence
			// as
			// per rfc4122, sec. 4.1.5
			for (i = 0; i < 36; i++) {
				if (!uuid[i]) {
					r = 0 | Math.random() * 16;
					uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
				}
			}
		}

		return uuid.join('');
	},
	  setFrameHeight: function(win, h){
	        var fr = this.getIframeByWindow(win);
	        if(!fr) {
	          return;
	        }
	        console.log(win.document.body.clientHeight, win.document.body.scrollHeight)
	        console.log(win.document.documentElement.clientHeight, win.document.documentElement.scrollHeight)
	        h = h || Math.max(win.document.body.clientHeight, win.document.body.scrollHeight);
	        $(fr).height(h);
	      },
	      
	      getIframeByWindow: function(win) {
	        var frs = document.getElementsByTagName("iframe");
	        for(var i = 0; i < frs.length; i++) {
	          try{
	            var w = frs[i].contentWindow;
	            if(w == win){
	              return frs[i]; 
	            }
	          }
	          catch(e){}
	        }
	        return null;
	      }
};

//改写alert对话框
;(function(){
  window._alert = window.alert;
  window.alert = function(text){
    if(!window.vueApp){
      _alert(text);
      return Promise.resolve();
    }
    else{
      return vueApp.$alert(text).catch(function(){
    	  return Promise.resolve();
      });
    }
  }
  
  //原生confirm是模态对话框,会阻塞js执行,但是elementui是弹出div层不会阻止js执行,需要在回调用执行代码,如果用elementui替换原生confirm需要修改js的处理方式
  // window._confirm = window.confirm;
  // window.confirm = function(text){
  //   //console.log(vueApp)
  //   if(!window.vueApp){
  //     _confirm(text);
  //   }
  //   else{
  //     return vueApp.$confirm(text);
  //   }    
  // }

  window.msgConfirm = function(text){
    if(!window.vueApp){
      return;
    }
    else{
      return vueApp.$confirm(text);
    }    
  }  

  window.msgInfo = function(text){
    if(!window.vueApp){
      return;
    }
    else{
      vueApp.$message({
        message: text,
      });
    }    
  }
  window.msgSuccess = function(text){
    if(!window.vueApp){
      return;
    }
    else{
      vueApp.$message({
        message: text,
        type: 'success'
      });
    }    
  }
  window.msgWarning = function(text){
    if(!window.vueApp){
      return;
    }
    else{
      vueApp.$message({
        message: text,
        type: 'warning'
      });
    }    
  }
  window.msgError = function(text){
    if(!window.vueApp){
      return;
    }
    else{
      vueApp.$message({
        message: text,
        type: 'error'
      });
    }    
  }

})();
//修改实例化后的axios, 这样可以不改变原来的axios配置, 更加灵活
var http = axios.create({
  timeout: 20000
})
http.interceptors.response.use(function (response) {
  return response.data.data;
}, function (error) {
  if (error.response) {
    console.log("ajax Error --> " + error.response.status + " --> " +  error.response.data);
  } else {
    console.log("ajax Error --> " + error.message);
  }
  return Promise.reject(error);
});

//此方法用于设置body的overflow为hidden,用在首页的tab上面,由于是给body加东西无法在vue里面实现,所以写在common.js里面
$(function(){
  var b = util.getQueryString("Htype");
  if(b == "N"){
    $("body").addClass("of-hidden");
    $(".wrap").removeClass("wrap");
  }
})
Number.prototype.toFixed = function( fractionDigits )
{
    //没有对fractionDigits做任何处理，假设它是合法输入
    return (parseInt(this * Math.pow( 10, fractionDigits  ) + 0.5)/Math.pow(10,fractionDigits)).toString();
}